import 'package:p_location/common/obj/dept_filter_option.dart';
import 'package:p_location/common/ui/base_filter_head_list_view.dart';
import 'package:p_location/home/constant/home_http_url.dart';
import 'package:p_location/home/obj/area_detail_info_item.dart';
import 'package:p_location/home/obj/area_person_obj.dart';
import 'package:p_location/home/res/tab_single_area_info_res.dart';
import 'package:p_location/home/widget/cell/cell_area_person_info.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/utils/string_util.dart';

class TabHomeAreaPersonInfo extends BaseFilterHeadListView {
  final bool isFromSearch;
  final bool isHiddenAppbar;
  final bool isHiddenBack;

  final int areaTreeId;

  @override
  BaseFilterHeadListViewState<BaseFilterHeadListView> getState() {
    return _FilterHeadListViewState();
  }

  const TabHomeAreaPersonInfo(this.areaTreeId,
      {this.isFromSearch = false,
      this.isHiddenAppbar = true,
      this.isHiddenBack = true})
      : super(isHiddenAppbar: isHiddenAppbar, isHiddenBack: isHiddenBack);
}

class _FilterHeadListViewState
    extends BaseFilterHeadListViewState<TabHomeAreaPersonInfo> {
  bool nextPageFlag = false;

  List<DeptFilterOption> typeOptions = [];
  List<DeptFilterOption> departOptions = [];
  List<DeptFilterOption> jobOptions = [];

  List survivedList = [];

  @override
  void initState() {
    super.initState();
  }

  @override
  initDropMenus() {
    initDropMenuView(0, [DeptFilterOption(deptCode: "全部", deptName: "全部")]);
    initDropMenuView(1, [DeptFilterOption(deptCode: "全部", deptName: "全部")]);
    initDropMenuView(2, [DeptFilterOption(deptCode: "全部", deptName: "全部")]);
  }

  @override
  List<String> getTabTitles() {
    return ['类型', '部门', '岗位'];
  }

  @override
  String getTitle() {
    return '车辆报警';
  }

  @override
  List<String> getFilterKeys() {
    return ['type', 'dept', 'job'];
  }

  @override
  bool hasNextPage() {
    return true;
  }

  @override
  getActions() {
    return [];
  }

  @override
  void realRequest() async {
    if (params != null) {
      if (StringUtil.isEmpty(params["type"]) &&
          StringUtil.isEmpty(params["dept"]) &&
          StringUtil.isEmpty(params["job"])) {
        params["areaTreeId"] = widget.areaTreeId;
        await DioHttpClient.requestPost(
            context,
            '${HomeHttpUrl.SINGLE_AREA_DETAIL_URL}?areaTreeId=${widget.areaTreeId}',
            params,
            this);
      }else if (params["type"] ==  "全部" && params["dept"] ==  "全部" && params["job"] ==  "全部"){
        isLoading = false;
        survivedList = lists;
      }else{
        survivedList = [];
        for (int i = 0; i < lists.length; i++) {
          AreaPersonObj element = lists[i];
          if (element != null) {
            if(StringUtil.isNotEmpty(params["type"]) && params["type"] != "全部"){
              if(element.type != params["type"]){
                continue;
              }
            }
            if(StringUtil.isNotEmpty(params["dept"]) && params["dept"] != "全部"){
              if(element.departName != params["dept"]){
                continue;
              }
            }
            if(StringUtil.isNotEmpty(params["job"]) && params["job"] != "全部"){
              if(element.jobName != params["job"]){
                continue;
              }
            }
            survivedList.add(element);
          }
        }
      }
    }

  }

  @override
  bool isInnerFilter() {
    return true;
  }

  @override
  int outerLength() {
    return survivedList?.length ?? 0;
  }

  @override
  List getList(response) {
    var resultList = [];
    if (response != null && response.data != null) {
      TabSingleAreaInfoRes listRes =
          TabSingleAreaInfoRes.fromJson(response.data);
      if (listRes != null && listRes.data != null) {
        AreaDetailInfoItem entire = listRes.data;
        if (entire.userList != null) {
          resultList.addAll(entire.userList);

          setState(() {
            typeOptions = [];
            departOptions = [];
            jobOptions = [];
            typeOptions.add(DeptFilterOption(deptCode: "全部", deptName: "全部"));
            departOptions.add(DeptFilterOption(deptCode: "全部", deptName: "全部"));
            jobOptions.add(DeptFilterOption(deptCode: "全部", deptName: "全部"));
            entire.userList.forEach((element) {
              if (StringUtil.isNotEmpty(element.type)) {
                DeptFilterOption find1 = typeOptions.firstWhere(
                    (e1) => e1.deptName == element.type,
                    orElse: () => null);
                if (find1 == null) {
                  typeOptions.add(DeptFilterOption(
                      deptCode: element.type, deptName: element.type));
                }
              }

              if (StringUtil.isNotEmpty(element.departName)) {
                DeptFilterOption find2 = departOptions.firstWhere(
                    (e1) => e1.deptName == element.departName,
                    orElse: () => null);
                if (find2 == null) {
                  departOptions.add(DeptFilterOption(
                      deptCode: element.departName,
                      deptName: element.departName));
                }
              }

              if (StringUtil.isNotEmpty(element.jobName)) {
                DeptFilterOption find3 = jobOptions.firstWhere(
                    (e1) => e1.deptName == element.jobName,
                    orElse: () => null);
                if (find3 == null) {
                  jobOptions.add(DeptFilterOption(
                      deptCode: element.jobName, deptName: element.jobName));
                }
              }
              initDropMenuView(0, typeOptions);
              initDropMenuView(1, departOptions);
              initDropMenuView(2, jobOptions);
            });
          });
        }
      }
    }
    survivedList = resultList;
    return resultList;
  }

  @override
  createListViewItem(index) {
    return CellAreaPersonInfo(survivedList[index]);
  }
}
